<template>
	<view>
		<view class="ipt-wrap">

			<view class="item-wrap">
				<view class="left">
					所属测站
				</view>
				<view class="right">
					<picker @change="PickerChange($event,'nameindex')" :value="nameindex" :range="arraysname">
						<view class="uni-input">{{arraysname[nameindex]}}</view>
					</picker>

				</view>
			</view>
			
			<view class="item-wrap">
				<view class="left">
					打卡类型
				</view>
				<view class="right">
					<view class="uni-input">{{clockType==1?'上班':'下班'}}</view>
			
				</view>
			</view>
			<!-- <view class="item-wrap">
				<view class="left">
					签到点位
				</view>
				<view class="right">
					{{location}}
			
				</view>
			</view> -->
			
			<view class="item-wrap">
				<view class="left">
					日期
				</view>
				<view class="right">
					{{specificDate}}


				</view>
			</view>
			<view class="item-wrap">
				<view class="left">
					时间
				</view>
				<view class="right">
					{{clockTime}}
			
				</view>
			</view>
			<!-- <view class="item-wrap">
				<view class="left">
					签到点坐标
				</view>
				<view class="right">
					<textarea v-model="maintenanceContent"  placeholder="请输入签到点坐标"   />
					
				</view>
			</view> -->
			
			
			
			<view class="item-wrap">
				<view class="left">
					备注
				</view>
				<view class="right">
					<input class="uni-input"  v-model="remark"  placeholder="请输入备注" />
				</view>
			</view>
			
			
		</view>
		<view class="sub-btn">
			
			<image @tap="subdata(true)" style="width: 268rpx; height: 112rpx;"  src="/static/imgnew/tjbg.png" />
		</view>
	</view>
</template>


<script>
	// import UNI_LOCATION from 'uni-location'
	export default{
		data(){
			return {
				arraysclockType:['上班','下班'],
				clockTypeindex:0,
				clockType:'',
				files:'',
				remark:'',
				clockTime:'',
				specificDate:'',
				arraysname:[],
				nameindex:0,
				user:{},
				coordinate:'',
				location:'',
				stcdList:[],
				pagedata:{}
				
			}
		},
		onLoad(options){
			let that=this
			// 使用setInterval定时获取位置信息
			setInterval(() => {
				console.log(1)
			    uni.getLocation({
					geocode:true,
			        type: 'wgs84',
			        success: (res) => {
						
						that.coordinate=res.latitude+'/'+res.longitude
						that.location=res.latitude+'/'+res.longitude
						
						// "city": "北京市",
						// "district": "东城区",
						// "street": "锡拉胡同",
						// "streetNum": "111号",
						// "poiName": "金鱼胡同(地铁站)",
			            console.log('setInterval success:', res)
			        },
					fail: function(res) { 
						
						uni.showToast({
						    title: res.errMsg,
						    duration:4000,
							icon:'none'
						});
						console.log(res) 
					},  
			    })
				// UNI_LOCATION.getLocation(options).then(res => {
				//     console.log('uni-location success:', res)
				// }).catch(err => {
				//     console.log('uni-location error:', err)
				// })
			}, 5000)
			const currentTime = new Date();
			const year = currentTime.getFullYear();
			const month = currentTime.getMonth() + 1; // 月份从0开始，因此需要加1
			const day = currentTime.getDate();
			const hours = currentTime.getHours();
			const minutes = currentTime.getMinutes();
			const seconds = currentTime.getSeconds();
			this.specificDate=`${year}-${month>9?month:'0'+month}-${day>9?day:'0'+day}`
			this.clockTime=`${hours>9?hours:'0'+hours}:${minutes>9?minutes:'0'+minutes}:${seconds>9?seconds:'0'+seconds}`
			
			this.clockType=hours<9?1:2
			this.user=JSON.parse(uni.getStorageSync("userinfo"))
			
			
			
			this.stcdList=this.user.stcdList
			
			
			if(options.pagedata){
				
				this.pagedata=JSON.parse(options.pagedata)
				this.clockType=this.pagedata.clockType
				this.location=this.pagedata.location
				this.pagedata.deviceNo=this.pagedata.deviceNo
				this.files=this.pagedata.files
				this.maintenanceContent=this.pagedata.maintenanceContent
				this.specificDate=this.pagedata.specificDate
				this.remark=this.pagedata.remark
				this.stType=this.pagedata.stType
				for(let i=0;i<this.stcdList.length;i++){
					if(this.stcdList[i].stcd==this.pagedata.stcd){
						this.nameindex=i
					}
				}
				this.stcd=this.pagedata.stcd
				
				
			}
			this.arraysname=this.stcdList.map(item=>{
				return item.stnm
			})
			
		},
		onNavigationBarButtonTap(e) {
			this.subdata()
		},
		
		onPullDownRefresh:function(){
			
			this.getData()
		 },
		onReachBottom() {
			
			this.getData()	
		},
		methods: {
			
			  
			
			PickerChange(val,type){
				this[type]=val.detail.value
			},
			subdata(){
				let that=this
				if(this.specificDate=='选择时间'){
					uni.showToast({
					    title: '请选择时间',
					    duration:4000,
						icon:'none'
					});
					return
				}
				if(this.maintenanceContent==''){
					uni.showToast({
					    title: '请输入维修内容',
					    duration:4000,
						icon:'none'
					});
					return
				}
				
				
				
				that.apipost('/soilWater/manage/attendance/save/saveAttendanceRecord',{
					clockTime:that.clockTime,
					clockType:that.clockType,
					//id:that.pagedata.id?that.pagedata.id:'',
					location:that.location,
					coordinate:that.coordinate,
					specificDate:that.specificDate,
					remark:that.remark,
					userName:that.user.username,
					stnm:that.stcdList[that.nameindex].stnm,
					stcd:that.stcdList[that.nameindex].stcd,
					userCode:that.user.userNo
				},false).then(res => {
					
					
					if(res.code==1){
						uni.showToast({
							icon: 'none',
							duration:4000,
							title: '操作成功'
						});
						setTimeout(function(){
							uni.navigateBack({
								delta:1
							})
						},800)
					}
					
				}).catch(err=>{
					console.log(err)
				});
			}
			
			
		},

	}
</script>
<style>
	.sub-btn{
		display:flex;
		justify-content: center;
		align-items: center;
		padding:40rpx 0 0 0;
	}
	.tip-wrap{
		margin-top:30rpx;
		line-height:54rpx;
		padding-left:40rpx;
	}
	page{
		height:100%;
		background:#F5F9FF;
	}
	.ipt-wrap{
		margin-top:80rpx;
		padding:0 40rpx;
		background:#fff;
	}
	.item-wrap{
		border-bottom:1rpx solid rgba(63,140,255,0.2);;
		color:#00041A;
		height:95rpx;
		display:flex;
		align-items: center;
		justify-content: space-between;
	}
	textarea,input{
		text-align: right;
	}
</style>